package com.lemon.funplus.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.funplus.BaseActivity;
import com.lemon.funplus.CommentActivity;
import com.lemon.funplus.DetailsTruyenActivity;
import com.lemon.funplus.R;
import com.lemon.funplus.VideoPlayActivity;
import com.lemon.funplus.app.AppController;
import com.lemon.funplus.object.DetailsCateObject;
import com.lemon.funplus.object.HomeObject;
import com.lemon.funplus.ulti.BaseEngine;
import com.lemon.funplus.view.FeedImageView;

@SuppressLint("InflateParams")
public class TruyenAdapter extends BaseAdapter {

	Context context;
	ArrayList<HomeObject> listHomeObjects;
	ImageLoader imageLoader;
	HomeObject homeObject;

	public TruyenAdapter(Context context, ArrayList<HomeObject> listHomeObjects) {
		this.context = context;
		this.listHomeObjects = listHomeObjects;
		imageLoader = ((BaseActivity)context).imageLoader;
	}

	@Override
	public int getCount() {
		return listHomeObjects.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.item_truyen, null);
			if (imageLoader == null)
				imageLoader = AppController.getInstance().getImageLoader();
			holder.layout = (LinearLayout) convertView.findViewById(R.id.layout_item_truyen);
			holder.progressBar = (ProgressBar) convertView.findViewById(R.id.home_progressBar_loading);
			holder.imageContent = (FeedImageView) convertView.findViewById(R.id.home_imageView_content);
			holder.textTitle = (TextView) convertView.findViewById(R.id.home_textView_title);
			holder.textMoretitle = (TextView) convertView.findViewById(R.id.home_textView_titlemore);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		homeObject = listHomeObjects.get(position);
		homeObject.showinfo();
		String urlImagecontent = "";
		// urlImagecontent = homeObject.getcontent();
		urlImagecontent = homeObject.getavatar();

		if (urlImagecontent != null) {
			holder.imageContent.setImageUrl(BaseEngine.checkUrlPath(urlImagecontent), imageLoader);
			holder.imageContent.setVisibility(View.VISIBLE);
			holder.imageContent.setResponseObserver(new FeedImageView.ResponseObserver() {
				@Override
				public void onError() {
					// holder.imageContent.setVisibility(View.GONE);
					holder.imageContent.setImageResource(R.drawable.image_error);
				}

				@Override
				public void onSuccess() {
					holder.progressBar.setVisibility(View.GONE);
				}
			});
		} else {
			holder.imageContent.setVisibility(View.GONE);
		}

		holder.textTitle.setText(homeObject.gettitle());
		holder.textMoretitle.setText(homeObject.getmoretitle());
		holder.layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent2 = new Intent(context, DetailsTruyenActivity.class);
				intent2.putExtra(DetailsTruyenActivity.ITEM_KEY, listHomeObjects.get(position));
				context.startActivity(intent2);
			}
		});
		return convertView;
	}

	@SuppressLint("NewApi")
	OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.home_imageView_comment:
				Intent intent = new Intent(context, CommentActivity.class);
				intent.putExtra(CommentActivity.FB_CM, homeObject.getfb_link());
				context.startActivity(intent);
				break;
			default:
				break;
			}

		}
	};

	private class ViewHolder {
		LinearLayout layout;
		FeedImageView imageContent;
		TextView textTitle, textMoretitle;
		ProgressBar progressBar;
	}
}